//
//  UIView+CQPopupAnimation.m
//  CJUIKitDemo
//
//  Created by ciyouzen on 15/11/12.
//  Copyright (c) 2015年 dvlproad. All rights reserved.
//

#import "UIView+CQPopupAnimation.h"
#import <CJPopupAnimation/UIView+CJPopupAnimation.h>

@implementation UIView (CQPopupAnimation)

#pragma mark - Public Method
/*
 *  从window外部的指定方向进来的动画(变化过程透明度会从0到1)
 *
 *  @param direction        从哪个方向进来的动画
 */
- (void)cj_animateFromWindowDirection:(CJDirection)direction {
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    CGRect convertFrame = [self.superview convertRect:self.frame toView:keyWindow];

    if (direction == CJDirectionTop) {
        CGFloat spacingToTop = CGRectGetMaxY(convertFrame);
        [self cj_animateForShow:YES withShowDirection:CJDirectionTop animateOffset:spacingToTop completion:nil];
        
    } else if (direction == CJDirectionBottom) {
        CGFloat spacingToBottom = CGRectGetMinY(convertFrame);
        [self cj_animateForShow:YES withShowDirection:CJDirectionBottom animateOffset:spacingToBottom completion:nil];
        
    } else if (direction == CJDirectionLeft) {
        CGFloat spacingToLeft = CGRectGetMaxX(convertFrame);
        [self cj_animateForShow:YES withShowDirection:CJDirectionLeft animateOffset:spacingToLeft completion:nil];
        
    } else if (direction == CJDirectionRight) {
        CGFloat spacingToRight = CGRectGetMinX(convertFrame);
        [self cj_animateForShow:YES withShowDirection:CJDirectionRight animateOffset:spacingToRight completion:nil];
    }
}

/*
 *  小距离的从指定方向进来的动画（默认移动40）(变化过程透明度会从0到1)
 *
 *  @param showFromDirection    是从哪个方向进来的(关闭的时候回到对应的方向)
 */
- (void)cq_animateSmallForShowWithDirection:(CJDirection)showFromDirection {
    [self __cq_animateSmallForShow:YES withShowDirection:showFromDirection];
}

/*
 *  小距离的离开的动画
 *
 *  @param animate              隐藏是否需要动画
 */
- (void)cq_animateSmallForHideWithAnimate:(BOOL)animate {
    if (animate) {
        CJDirection showFromDirection = self.cjShowFromDirection;
        [self __cq_animateSmallForShow:NO withShowDirection:showFromDirection];
    } else {
        self.alpha = 0;
    }
}

/*
 *  小距离的从指定方向进来的动画（默认移动40）(变化过程透明度会从0到1)
 *
 *  @param forShow              显示还是隐藏
 *  @param showFromDirection    如果是显示的话，是从哪个方向进来的(关闭的时候回到对应的方向)
 */
- (void)__cq_animateSmallForShow:(BOOL)forShow withShowDirection:(CJDirection)showFromDirection {
    CGFloat animateOffset = 40; // 移动的距离（已规定是要正数）
    [self cj_animateForShow:forShow withShowDirection:showFromDirection animateOffset:animateOffset completion:nil];
}

@end
